﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DataModel;
using BasicDataManagement;
using System.Drawing.Printing;

namespace CheckStockManagement
{
    public partial class PurchaseTableForm : Form
    {
        List<MaterialObject> matList1;
        List<MaterialObject> matList2;
        List<PurchaseTableInfo> purList;
        PrintDocument printDocument;


        OpFromDB op;

        public PurchaseTableForm(List<MaterialObject> mat1, List<MaterialObject> mat2)
        {
            InitializeComponent();
            //构造PrintDocument对象添加打印事件

            printDocument = new PrintDocument();
            //printDocument.PrintPage += new PrintPageEventHandler(this.printDocument_PrintPage);


            matList1 = mat1;
            matList2 = mat2;
            op = OpFromDB.GetInstance();
            purList = new List<PurchaseTableInfo>();

            for (int i = 0; i < mat1.Count; i++)
            {
                if (mat2[i].Stock == 0) {
                    continue;
                }
                purTablelistView.Items.Add(new ListViewItem(new string[] { mat1[i].ObjectName, mat1[i].Unit, mat1[i].Stock.ToString(), mat2[i].Stock.ToString(), (mat2[i].Stock - mat1[i].Stock).ToString() }));
                PurchaseTableInfo pt = new PurchaseTableInfo();
                pt.Date = DateTime.Now;
                pt.MaterialName = mat1[i].ObjectName;
                pt.Unit = mat1[i].Unit;
                pt.OldStock = mat1[i].Stock;
                pt.NowWantStock = mat2[i].Stock;
                pt.NowPurchaseStock = pt.NowWantStock - pt.NowWantStock;
                purList.Add(pt);

            }
        }
        //打印
        private void Printbutton_Click(object sender, EventArgs e)
        {
            foreach (PurchaseTableInfo p in purList)
            {
                op.AddPurchaseTable(p.Date, p.MaterialName, p.Unit, p.OldStock, p.NowWantStock, p.NowPurchaseStock);
            }
        }


        //实现打印事件功能
        //打印和绘图类似都是调用Graphics 类的方法进行画图 不同的是一个在显示器上一个在打印纸上并且打印要进行一些复杂的计算
        //如换行 分页等。 
      /*  private void printDocument_PrintPage(object sender, PrintPageEventArgs e)
    {
Graphics g = e.Graphics; //获得绘图对象
float linesPerPage = 0; //页面的行号
float yPosition = 0;   //绘制字符串的纵向位置
int count = 0; //行计数器
float leftMargin = e.MarginBounds.Left; //左边距
float topMargin = e.MarginBounds.Top; //上边距
string line = null; //行字符串
Font printFont = this.textBox.Font; //当前的打印字体
SolidBrush myBrush = new SolidBrush(Color.Black);//刷子
linesPerPage = e.MarginBounds.Height //
    printFont.GetHeight(g);//每页可打印的行数
//逐行的循环打印一页
    while(count < linesPerPage && ((line=lineReader.ReadLine()) != null))
    {
       yPosition = topMargin + (count * printFont.GetHeight(g));
       g.DrawString(line, printFont, myBrush, leftMargin, yPosition, new StringFormat());
       count++;
    }

        }*/
    }
}
